package com.kingmed.kmss.common.utils;

import com.kingmed.kmss.common.annotation.CheckField;
import com.kingmed.kmss.common.enums.base.FieldType;
import com.kingmed.kmss.common.exception.BusinessException;
import lombok.extern.slf4j.Slf4j;

import java.lang.reflect.Field;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Slf4j
public class CheckFieldUtils {

    public static void valid(List<Object> list) {
        for (Object o : list) {
            valid(o);
        }
    }

    public static void valid(Object object) {
        // 获取object的类型
        Class<? extends Object> clazz = object.getClass();
        // 获取该类型声明的成员
        Field[] fields = clazz.getDeclaredFields();
        //循环遍历对象中的属性
        for (Field field : fields) {
            // 获取对象的成员的注解信息
            CheckField validator = field.getAnnotation(CheckField.class);
            if (validator == null) {
                continue;
            }
            if (!field.isAccessible()) {
                field.setAccessible(true);
            }
            try {
                Object value = field.get(object);
                validField(value, validator, field, object);
            } catch (Exception e) {
                throw new BusinessException(e.getMessage());
            }
        }
    }

    private static void validField(Object value, CheckField validator, Field field, Object object) {
        if (value instanceof String) {
            String valueStr = (String) value;
            if (FieldType.PHONE == validator.fieldType()) {
                checkPhone(valueStr, validator);
            }
            if (FieldType.EMAIL == validator.fieldType()) {
                checkEmail(valueStr, validator);
            }
        }
    }

    /**
     * 校验手机号
     */
    private static void checkPhone(String phone, CheckField validator) {
        if (StringUtils.isBlank(phone)) {
            return;
        }
        if (phone.length() != 11) {
            throw new BusinessException(validator.msg());
        }
        String regex = "^[1]([3-9])[0-9]{9}$";
        Pattern p = Pattern.compile(regex);
        Matcher m = p.matcher(phone);
        if (!m.matches()) {
            throw new BusinessException(validator.msg());
        }
    }

    /**
     * 校验邮箱
     */
    private static void checkEmail(String email, CheckField validator) {
        if (StringUtils.isBlank(email)) {
            return;
        }
        // 判断是否为空邮箱
        int k = 0;
        // 判断是否有仅有一个@且不能在开头或结尾
        if (email.indexOf("@") > 0 && email.indexOf('@') == email.lastIndexOf('@') && email.indexOf('@') < email.length() - 1) {
            k++;
        }
        // 判断"@"之后必须有"."且不能紧跟
        if (email.indexOf('.', email.indexOf('@')) > email.indexOf('@') + 1) {
            k++;
        }
        // 判断"@"之前或之后不能紧跟"."
        if (email.indexOf('.') < email.indexOf('@') - 1 || email.indexOf('.') > email.indexOf('@') + 1) {
            k++;
        }
        // @之前要有6个字符
        if (email.indexOf('@') > 5) {
            k++;
        }
        if (email.endsWith("com") || email.endsWith("org") || email.endsWith("cn") || email.endsWith("net")) {
            k++;
        }
        if (k != 5) {
            throw new BusinessException(validator.msg());
        }
    }

}
